//
//  YWHeaderView.m
//  聆听FM
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWHeaderView.h"
#import "LTDataBase.h"
#import "LTAnchorModel.h"

@implementation YWHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -45, SCREENWIDTH, self.bounds.size.height + 45)];
        [self addSubview:_backImageView];
        UIView *backView = [[UIView alloc] initWithFrame:self.bounds];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha = 0.3;
        [self addSubview:backView];
        
        self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backButton.frame = CGRectMake(20, 30, 20, 20);
        [_backButton setImage:[UIImage imageNamed:@"whiteBack"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(didClickedReturnButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_backButton];
        
        self.headView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 33 * FITWIDTH, 40 * FITHEIGHT, 66 * FITWIDTH, 66 * FITWIDTH)];
        _headView.backgroundColor = [UIColor whiteColor];
        _headView.alpha = 0.5;
        _headView.layer.cornerRadius = 33;
        [self addSubview:_headView];
        
        self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 31 * FITWIDTH, 40 * FITHEIGHT + 2 * FITWIDTH, 62 * FITWIDTH, 62 * FITWIDTH)];
        _headImageView.layer.cornerRadius = 31;
        _headImageView.layer.masksToBounds = YES;
        [self addSubview:_headImageView];
        
        self.tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10000, 30)];
        _tempLabel.font = [UIFont systemFontOfSize:16];

        self.nickNameLabel = [[UILabel alloc] init];
        _nickNameLabel.textColor = [UIColor whiteColor];
        _nickNameLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_nickNameLabel];
        
        self.vImageView = [[UIImageView alloc] init];
        [self addSubview:_vImageView];
        
        self.containLabel = [[UILabel alloc] initWithFrame:CGRectMake(70 * FITWIDTH, 150 * FITHEIGHT, 235 * FITWIDTH, 20 * FITHEIGHT)];
        _containLabel.font = [UIFont systemFontOfSize:13];
        _containLabel.textColor = [UIColor whiteColor];
        _containLabel.textAlignment = 1;
        [self addSubview:_containLabel];
        
        self.containHiddenLabel = [[UILabel alloc] initWithFrame:CGRectMake(70 * FITWIDTH, 170 * FITHEIGHT, 235 * FITWIDTH, 0)];
        _containHiddenLabel.font = [UIFont systemFontOfSize:13];
        _containHiddenLabel.textColor = [UIColor whiteColor];
        _containHiddenLabel.textAlignment = 1;
        _containHiddenLabel.numberOfLines = 0;
        [self addSubview:_containHiddenLabel];
        
        self.downArrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(177.5 * FITWIDTH, 180 * FITHEIGHT, 20 * FITWIDTH, 20 * FITWIDTH)];
        [self addSubview:_downArrowImageView];
        _downArrowImageView.tag = 1000;
        
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 210 * FITHEIGHT, SCREENWIDTH, 90 * FITHEIGHT)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [self addSubview:whiteView];
        
        self.attentionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(57 * FITWIDTH, 10 * FITHEIGHT, 261 * FITWIDTH, 34 * FITHEIGHT)];
        _attentionImageView.userInteractionEnabled = YES;
        _attentionImageView.tag = 1001;
        [whiteView addSubview:_attentionImageView];
        
        UITapGestureRecognizer *attentionTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAttentionImageView:)];
        [_attentionImageView addGestureRecognizer:attentionTap];
        
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(187 * FITWIDTH, 55 * FITHEIGHT, 1 * FITWIDTH, 25 * FITHEIGHT)];
        lineLabel.backgroundColor = [UIColor colorWithWhite:0.677 alpha:1.000];
        [whiteView addSubview:lineLabel];
        
        self.followingsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * FITHEIGHT, 187 * FITWIDTH, 15 * FITHEIGHT)];
        _followingsLabel.userInteractionEnabled = YES;
        _followingsLabel.tag = 1050;
        UITapGestureRecognizer *followingsLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFollowLabel:)];
        [_followingsLabel addGestureRecognizer:followingsLabelTap];
        _followingsLabel.font = [UIFont systemFontOfSize:14];
        _followingsLabel.textAlignment = 1;
        [whiteView addSubview:_followingsLabel];
        
        self.followingsTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70 * FITHEIGHT, 187 * FITWIDTH, 15 * FITHEIGHT)];
        _followingsTextLabel.userInteractionEnabled = YES;
        _followingsTextLabel.tag = 1051;
        UITapGestureRecognizer *followingsTextLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFollowLabel:)];
        [_followingsTextLabel addGestureRecognizer:followingsTextLabelTap];
        _followingsTextLabel.font = [UIFont systemFontOfSize:14];
        _followingsTextLabel.textAlignment = 1;
        _followingsTextLabel.textColor = [UIColor grayColor];
        [whiteView addSubview:_followingsTextLabel];
        
        self.followersLabel = [[UILabel alloc] initWithFrame:CGRectMake(188 * FITWIDTH, 50 * FITHEIGHT, 187 * FITHEIGHT, 15 * FITHEIGHT)];
        _followersLabel.userInteractionEnabled = YES;
        _followersLabel.tag = 1052;
        UITapGestureRecognizer *followersLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFollowLabel:)];
        [_followersLabel addGestureRecognizer:followersLabelTap];
        _followersLabel.font = [UIFont systemFontOfSize:14];
        _followersLabel.textAlignment = 1;
        [whiteView addSubview:_followersLabel];
        
        self.followersTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(188 * FITWIDTH, 70 * FITHEIGHT, 187 * FITHEIGHT, 15 * FITHEIGHT)];
        _followersTextLabel.userInteractionEnabled = YES;
        _followersTextLabel.tag = 1053;
        UITapGestureRecognizer *followersTextLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFollowLabel:)];
        [_followingsTextLabel addGestureRecognizer:followersTextLabelTap];
        _followersTextLabel.font = [UIFont systemFontOfSize:14];
        _followersTextLabel.textAlignment = 1;
        _followersTextLabel.textColor = [UIColor grayColor];
        [whiteView addSubview:_followersTextLabel];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapDownImage:)];
        [_downArrowImageView addGestureRecognizer:tap];
        [self addObserver:self forKeyPath:@"attentionImageView.tag" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"attentionImageView.tag"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"attentionImageView.tag"]) {
        NSInteger num = [[change objectForKey:@"new"] integerValue];
        if (num == 2001) {
           _attentionImageView.image = [UIImage imageNamed:@"btn_follow_n"];
        }
        if (num == 1001)
        {
             _attentionImageView.image = [UIImage imageNamed:@"btn_unfollow_n"];
        }
    }
}


- (void)didClickedReturnButton:(UIButton *)sender
{
    [_delegate returnLastVC];
}

- (void)didTapAttentionImageView:(UITapGestureRecognizer *)tap
{
     LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    if (tap.view.tag == 1001) {
        
        [MBProgressHUD showSuccess:@"已关注"];
        LTAnchorModel *model = [[LTAnchorModel alloc] init];
        model.uid = self.messageModel.uid;
        model.nickname = self.messageModel.nickname;
        model.smallLogo = self.messageModel.mobileLargeLogo;
        model.title = self.messageModel.ptitle;
        [data insertDataWithAnchorModel:model];
        self.attentionImageView.tag = 2001;
    }
    else
    {
       
        [MBProgressHUD showSuccess:@"已取消关注"];
        [data delegateDataWithModel_id:self.messageModel.uid type:3];
        self.attentionImageView.tag = 1001;
    }
}

- (void)didTapFollowLabel:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1050 || tap.view.tag == 1051) {
        [_enterFollowingDelegate enterFollowVC:@"following"];
    }
    else
    {
        [_enterFollowingDelegate enterFollowVC:@"follower"];
    }
}

- (void)setMessageModel:(YWAnchorMessageModel *)messageModel
{
    if (_messageModel != messageModel) {
        _messageModel = messageModel;
        [_backImageView sd_setImageWithURL:[NSURL URLWithString:messageModel.backgroundLogo] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:messageModel.mobileLargeLogo] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
        NSString *string = _messageModel.nickname;
        _tempLabel.frame = CGRectMake(0, 0, 10000, 30);
        _tempLabel.text = string;
        [_tempLabel sizeToFit];
        _nickNameLabel.frame = CGRectMake(self.frame.size.width / 2 - 10 * FITWIDTH - _tempLabel.bounds.size.width / 2, 120 * FITHEIGHT, _tempLabel.bounds.size.width, 30);
        _nickNameLabel.text = messageModel.nickname;
        _vImageView.frame = CGRectMake(_nickNameLabel.frame.origin.x + _nickNameLabel.bounds.size.width + 5 * FITWIDTH, _nickNameLabel.frame.origin.y + 3 * FITHEIGHT, 20 * FITWIDTH, 20 * FITWIDTH);
        _vImageView.image = [UIImage imageNamed:@"V"];
        _containLabel.text = messageModel.personalSignature;
        _containHiddenLabel.text = messageModel.personalSignature;
        _downArrowImageView.image = [UIImage imageNamed:@"DownArrow"];
        _downArrowImageView.userInteractionEnabled = YES;
        _attentionImageView.image = [UIImage imageNamed:@"btn_unfollow_n"];
        _followingsLabel.text = [NSString stringWithFormat:@"%ld", messageModel.followings];
        _followingsTextLabel.text = @"关注的人";
        NSInteger num = messageModel.followers;
        if (num < 10000) {
            _followersLabel.text = [NSString stringWithFormat:@"%ld", num];
        }
        else
        {
            if (num % 10000 == 0) {
                _followersLabel.text = [NSString stringWithFormat:@"%ld万", num / 10000];
            }
            else
            {
                _followersLabel.text = [NSString stringWithFormat:@"%.1f万", num / 10000.0];
            }
            
        }
        _followersTextLabel.text = @"粉丝";
        LTDataBase *data = [LTDataBase shareLTDataBaseTool];
        NSInteger tempNum = [data isSaveDataInTableWithModel_id:messageModel.uid type:3];
        if (tempNum == 0) {
            self.attentionImageView.tag = 2001;
        }
        else
        {
            self.attentionImageView.tag = 1001;
        }
    }
}

- (void)didTapDownImage:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1000) {
        [UIView animateWithDuration:0.5 animations:^{
            _downArrowImageView.transform = CGAffineTransformRotate(_downArrowImageView.transform, M_PI);
            _headImageView.transform = CGAffineTransformRotate(_headImageView.transform, M_PI);
            _headView.frame = CGRectMake(self.frame.size.width / 2 - 33 * FITWIDTH, -66 * FITWIDTH, 66 * FITWIDTH, 66 * FITWIDTH);
            _headView.alpha = 0;
            _headImageView.frame = CGRectMake(self.frame.size.width / 2 - 31 * FITWIDTH, -62 * FITWIDTH, 62 * FITWIDTH, 62 * FITWIDTH);
            _headImageView.alpha = 0;
            _nickNameLabel.frame = CGRectMake(self.frame.size.width / 2 - 10 * FITWIDTH - _tempLabel.bounds.size.width / 2, 120 * FITHEIGHT - 80 * FITHEIGHT, _tempLabel.bounds.size.width, 30);
            _vImageView.frame = CGRectMake(_nickNameLabel.frame.origin.x + _nickNameLabel.bounds.size.width + 5 * FITWIDTH, _nickNameLabel.frame.origin.y + 3 * FITHEIGHT , 20 * FITWIDTH, 20 * FITWIDTH);
            _containLabel.hidden = YES;
            _containHiddenLabel.alpha = 1;
            _containHiddenLabel.frame = CGRectMake(70 * FITWIDTH, 150 * FITHEIGHT - 80 * FITHEIGHT, 235 * FITWIDTH, 100 * FITHEIGHT);
        }];
        _downArrowImageView.tag = 2000;
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            _downArrowImageView.transform = CGAffineTransformRotate(_downArrowImageView.transform, M_PI);
            _headImageView.transform = CGAffineTransformRotate(_headImageView.transform, M_PI);
            _headView.frame = CGRectMake(self.frame.size.width / 2 - 33 * FITWIDTH, 40 * FITHEIGHT, 66 * FITWIDTH, 66 * FITWIDTH);
            _headView.alpha = 0.5;
            _headImageView.frame = CGRectMake(self.frame.size.width / 2 - 31 * FITWIDTH, 40 * FITHEIGHT + 2 * FITWIDTH, 62 * FITWIDTH, 62 * FITWIDTH);
            _headImageView.alpha = 1;
            _nickNameLabel.frame = CGRectMake(self.frame.size.width / 2 - 10 * FITWIDTH - _tempLabel.bounds.size.width / 2, 120, _tempLabel.bounds.size.width, 30);
            _vImageView.frame = CGRectMake(_nickNameLabel.frame.origin.x + _nickNameLabel.bounds.size.width + 5 * FITWIDTH, _nickNameLabel.frame.origin.y + 3 * FITHEIGHT, 20 * FITWIDTH, 20 * FITWIDTH);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                _containLabel.hidden = NO;
            });
            _containHiddenLabel.alpha = 0;
            _containHiddenLabel.frame = CGRectMake(70 * FITWIDTH, 170 * FITHEIGHT, 235 * FITWIDTH, 0);
        }];
        _downArrowImageView.tag = 1000;
    }
}

@end
